package sg.techstudio.suntec.controller;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import sg.techstudio.suntec.dao.EventContactDao;
import sg.techstudio.suntec.dao.RadiusDao;
import sg.techstudio.suntec.object.CPLoginObj;
import sg.techstudio.suntec.object.MacSession;
import sg.techstudio.suntec.validator.CpValidatorMvc;

@Controller
public class CPController extends BaseController {
	private static final Log log = LogFactory.getLog(CPController.class);
	private static final String FREE_USERNAME = "free";

	@Override
	protected String getModuleFolder() {
		return "cp/";
	}
	
	@Autowired
	private CpValidatorMvc cpValidatorMvc;
	
	@Autowired
	private RadiusDao radiusDao;
	
	@Autowired
	private EventContactDao eDao;
	
	@RequestMapping(value="cpthankyou"+SPRING_EXT)
	public String thankyou(Model model) {
		log.debug("thankyou() entry");
		return goToPageJsp("cpthankyou");
	}
	
	@RequestMapping(value="cpfree"+SPRING_EXT)
	public String free(Model model) {
		log.debug("thankyou() entry");
		
		CPLoginObj cp = new CPLoginObj();
		model.addAttribute("cp", cp);
		
		return goToPageJsp("freeSplash");
	}
	
	@RequestMapping(value="cpentry"+SPRING_EXT)
	public String cpentry(Model model
			, @RequestParam(value="wlan", required=false) String ssid
			, @RequestParam(value="ap_mac", required=false) String apName
			, @RequestParam(value="vcname", required=false) String vcName
			, @RequestParam(value="client_mac", required=false) String mac
			, @RequestParam(value="ip", required=false) String ip
			, @RequestParam(value="cmd", required=false) String cmd
			, @RequestParam(value="redirect", required=false) String url
			, HttpSession session) {
		log.debug("cpwrapper() entry");
		log.debug("input ssid:"+ssid);
		
		CPLoginObj cp = new CPLoginObj();
		cp.setSsid(ssid);
		cp.setApName(apName);
		cp.setVcName(vcName);
		cp.setMac(mac);
		cp.setIp(ip);
		cp.setCmd(cmd);
		cp.setUrl(url);
		model.addAttribute("cp", cp);
		
		return goToPageJsp(isFree(ssid)?"freeSplash":"loginForm");
	}
	
	@RequestMapping(value="cpsubmit"+SPRING_EXT)
	public String cpsubmit(Model model
			, CPLoginObj cp
			, BindingResult bindingResult) {
		log.debug("cpsubmit() entry");
		
		model.addAttribute("cp", cp);
		boolean isFreeSsid = isFree(cp.getSsid());
		if(!isFreeSsid) {
//			radiusDao.deleteInactiveSession(cp.getUsername(), 24);
			//delegate to reuse validation from webflow
			cpValidatorMvc.validateLoginForm(cp, model);
			if(hasErrorMsg(model))
				return goToPageJsp("loginForm");
		}
//		String mac = StringUtils.remove(cp.getMac(), ':');
		
//		MacSession ms = radiusDao.getMacSession(cp.getMac());
		MacSession ms = createMacSession(null, cp);
		radiusDao.saveMacSession(ms);
		
		return goToPageJsp("redirectLogin");
	}
	
	protected MacSession createMacSession(MacSession ms, CPLoginObj cp) {
		if(ms==null)
			ms = new MacSession();
		ms.setMac(cp.getMac());
		ms.setSsid(cp.getSsid());
		ms.setApMac(cp.getApName());
		ms.setGroupname(ms.getSsid());
		ms.setUrl(cp.getUrl());
//		ms.setGroupname(getGroupName(cp.getSsid()));
		
		if(isFree(cp.getSsid())) {
			ms.setUsername(FREE_USERNAME);
			ms.setEndDate(null);
		} else {
			ms.setUsername(cp.getPassword());
//			WifiAccountObj wa = eDao.getWifiAccountObj(cp.getUsername());
//			RestEventContactObj ec = eDao.findEventByWifiAccount(wa);
//			ms.setUsername(cp.getUsername());
//			if(ec!=null)
//				ms.setEndDate(ec.getActivateEndDate());
		}
		
		return ms;
	}
	
	protected MacSession createMacSession(String mac, String username, String groupname, String ssid) {
		MacSession ms = new MacSession();
		ms.setMac(mac);
		ms.setUsername(username);
		ms.setGroupname(groupname);
		ms.setSsid(ssid);
		return ms;
	}
	protected boolean isFree(String ssid) {
		return StringUtils.equalsIgnoreCase(ssid, "SA2014");
	}
	protected String getGroupName(String ssid) {
		if(cpValidatorMvc.isGoldSsid(ssid))
			return "gold";
		if(cpValidatorMvc.isSilverSsid(ssid))
			return "silver";
		if(cpValidatorMvc.isChcSsid(ssid))
			return "chc";
		return "free";
	}
}
